package com.takanafu.haru.code{
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	public class CodeHandler extends CodeBase{
		protected static var table:Dictionary = new Dictionary();
		
		public function hasSymbol(sym:CodeSymbol):Boolean{
			if(table.hasOwnProperty(sym) == true){
				return true;
			}
			return false;
		}
		public function getSymbolValue(sym:CodeSymbol):CodeBase{
			if(table.hasOwnProperty(sym) == true){
				return table[sym];
			}
			return null;
		}
		override public function message(symbol:CodeSymbol):CodeBase{
			if(hasSymbol(symbol) == false){
				return super.message(symbol);
			}
			var c:CodeBase = getSymbolValue(symbol);
			
			if(c is CodeFunction == true){
				throw new Error("method.not field");
			}
			return c;
		}
		override public function method(symbol:CodeSymbol,list:ArrayCollection):CodeBase{
			if(hasSymbol(symbol) == false){
				return super.method(symbol,list);
			}
			var c:CodeBase = getSymbolValue(symbol);
			
			if(c is CodeFunction == false){
				throw new Error("not method");
			}
			var f:CodeFunction = c as CodeFunction;
			if(list == null){
				list = new ArrayCollection();
			}
			list.addItemAt(this,0);
			return f.call(list);
		}
	}
}